package com.petadoption.serverclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.petadoption.serverclient.entity.User;
import com.petadoption.serverclient.mapper.UserMapper;
import com.petadoption.serverclient.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User findByUserPhone(User users) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("user_phone", users.getUserPhone()).and(userQueryWrapper -> userQueryWrapper.eq("user_password", users.getUserPassword()));
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Integer addUser(User users) {
        return userMapper.insert(users);
    }

    @Override
    public User findUser(User users) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone", users.getUserPhone());
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findNameByPhone(String phone) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("user_phone", phone));
    }

    @Override
    public User findUserById(Integer userId) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("user_id", userId));
    }

    @Override
    public int updateUserAvatar(User user) {
        return userMapper.update(user, new UpdateWrapper<User>().eq("user_id", user.getUserId()));
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
//        return userMapper.update(user, new UpdateWrapper<User>().eq("user_id", user.getUserId()));
    }

}
